package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateTimeHelper {
	public static final String DEFAULT = "MM/dd/yyyy hh:mm:ss";
	public static final String DATE_FORMAT_DISPLAY = "MMM dd, yyyy";
	public static final String TIME_FORMAT_DISPLAY = "hh:mm aa";
	public static final String DATE_FORMAT = "yyyyMMdd";
    public static final String TIME_FORMAT = "HHmmss";
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);
    private static final SimpleDateFormat dateFormatToString = new SimpleDateFormat(DATE_FORMAT_DISPLAY);
    private static final SimpleDateFormat timeFormatToString = new SimpleDateFormat(TIME_FORMAT_DISPLAY);
    
    public static int formatTimeAsInt(Calendar cal){
    	return Integer.parseInt(timeFormat.format(cal.getTime()));
    }
    
    public static int formatDateAsInt(Calendar cal){
    	return Integer.parseInt(dateFormat.format(cal.getTime()));
    }
    
    public static Calendar getCalendarFromFormattedDateInt(int l){
    	try {
			Calendar c = Calendar.getInstance();
			c.setTime(dateFormat.parse(String.valueOf(l)));
			return c;
			
		} catch (ParseException e) {
			return null;
		}
    }
    
    public static Calendar getCalendarFromFormattedTimeInt(int l){
    	try {
			Calendar c = Calendar.getInstance();
			c.setTime(timeFormat.parse(String.valueOf(l)));
			return c;
			
		} catch (ParseException e) {
			return null;
		}
    }
    
    public static int getNowFormattedInt(){
    	return DateTimeHelper.formatDateAsInt(Calendar.getInstance());
    }
    
	public static String getFormattedTime(int time){
		Calendar calendar = DateTimeHelper.getCalendarFromFormattedTimeInt(time);
		return timeFormatToString.format(calendar.getTime());
	}
	
	public static String getFormattedDate(int date){
		Calendar calendar = DateTimeHelper.getCalendarFromFormattedDateInt(date);
		return dateFormatToString.format(calendar.getTime());
	}
}
